#!/usr/bin/env bash

set -o xtrace

# Keep track of the current devstack directory.
TOP_DIR=$(cd $(dirname "$0") && pwd)

CC_PUBLIC_IP=${CC_PUBLIC_IP:-"192.168.168.69"}
CC_PRIVATE_IP=${CC_PRIVATE_IP:-"192.168.3.69"}
PUBLIC_IP=`ip a show dev eth0 | grep 'inet ' | awk '{print $2}' | cut -d\/ -f1`
PRIVATE_IP=${PRIVATE_IP:-"192.168.3.66"}
PRIVATE_PREFIX=${PRIVATE_PREFIX:-"192.168.3"}
FIXED_RANGE_PREFIX=${FIXED_RANGE_PREFIX:-"172.20.1"}

# /apt/source/source.list
SOURCE_FILE=${SOURCE_FILE:-"/etc/apt/sources.list"}
sed -i 's/us.archive.ubuntu.com/mirrors.163.com/g' $SOURCE_FILE
sudo apt-get update

# bridge-utils
sudo apt-get install -y bridge-utils
	
# network configure
NETWORK_CONF=${NETWORK_CONF:-"/etc/network/interfaces"}
DNS_CONF=${DNS_CONF:-"/etc/resolv.conf"}
DNS_ADDRESS=${DNS_ADDRESS:-"202.119.32.6"}
if ! grep -q eth1 $NETWORK_CONF; then
	echo "auto eth1" >> $NETWORK_CONF
	echo "iface eth1 inet static" >> $NETWORK_CONF
	echo "    address $PRIVATE_IP" >> $NETWORK_CONF
	echo "    netmask 255.255.255.0" >> $NETWORK_CONF
	echo "    network $PRIVATE_PREFIX.0" >> $NETWORK_CONF
	echo "    broadcast $PRIVATE_PREFIX.255" >> $NETWORK_CONF
	sudo /etc/init.d/networking restart
fi

if ! grep -q $DNS_ADDRESS $DNS_CONF; then
	echo "nameserver $DNS_ADDRESS" >> $DNS_CONF
fi

# ntp
sudo apt-get install -y ntp
if ! grep -q fudge "/etc/ntp.conf"; then
	sudo sed -i 's/server ntp.ubuntu.com/server ntp.ubuntu.com\nserver '"$CCIP"'' /etc/ntp.conf
	sudo service ntp restart
fi

# on compute node
ntpdate $CC_PUBLIC_IP
hwclock -w

# MySQL
MYSQL_PASS=${MYSQL_PASS:-"hezudaopp"}
	
# nova
NOVA_USERNAME=${NOVA_USERNAME:-"novadbadmin"}
NOVA_PASS=${NOVA_PASS:-"hezudaopp"}
sudo apt-get install -y nova-common python-nova nova-compute vlan
sudo apt-get install -y euca2ools
sudo apt-get install -y unzip
	
# nova configuration
NOVA_CONF_DIR=${NOVA_CONF_DIR:-"/etc/nova"}
NOVA_CONF=${NOVA_CONF:-"nova.conf"}
rm -f $NOVA_CONF_DIR/$NOVA_CONF

echo "--dhcpbridge_flagfile=/etc/nova/nova.conf" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--dhcpbridge=/usr/bin/nova-dhcpbridge" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--logdir=/var/log/nova" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--state_path=/var/lib/nova" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--lock_path=/var/lock/nova" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--flagfile=/etc/nova/nova-compute.conf" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--use_deprecated_auth" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--verbose" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--s3_host=$CC_PUBLIC_IP" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--rabbit_host=$CC_PUBLIC_IP" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--cc_host=$CC_PUBLIC_IP" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--network_size=8" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--routing_source_ip=$CC_PUBLIC_IP" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--sql_connection=mysql://$NOVA_USERNAME:$NOVA_PASS@$CC_PUBLIC_IP/nova" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--glance_api_servers=$CC_PRIVATE_IP:9292" >> $NOVA_CONF_DIR/$NOVA_CONF
echo "--image_service=nova.image.glance.GlanceImageService" >> $NOVA_CONF_DIR/$NOVA_CONF

#do we support KVM?
if ! sudo kvm-ok 1>/dev/null 2>&1; then
	sudo sed -i -e "s/kvm/qemu/" /etc/nova/nova-compute.conf
fi

sudo chown -R root:nova $NOVA_CONF_DIR
sudo chmod 644 $NOVA_CONF_DIR/$NOVA_CONF

# restart all service
sudo restart nova-compute
if ! ps aux | grep nova-compute | grep -q -v grep; then sudo start nova-compute; fi

# euca env
sudo scp root@$CC_PUBLIC_IP:$TOP_DIR/novarc $TOP_DIR
if ! grep -q "$TOP_DIR/novarc" /root/.bashrc; then
	echo "source $TOP_DIR/novarc" >> /root/.bashrc
fi

exit 0